package com.crl.module.connecter.handler
{
	import com.crl.module.connecter.core.INetSender;
	import com.crl.module.connecter.decoder.AbstractDecoder;
	import com.crl.module.connecter.decoder.IDecoder;
	import com.crl.module.connecter.model.CommandInfo;
	
	import flash.utils.Dictionary;
	
	/**
	 * 抽像解析路由;
	 * @author crl
	 * 
	 */	
	public class AbstractNetHandler implements INetHandler
	{
		protected var decoders:Dictionary;
		public function AbstractNetHandler()
		{
			decoders=new Dictionary();
		}
		
		public function addDecoder(decoder:IDecoder):void
		{
			var type:String=decoder.getType();
			
			var old:IDecoder=decoders[type];
			if(old ==decoder)return;
			
			old.manager=null;
			decoders[type]=decoder;
			decoder.manager=this;
		}
		
		public function removeDecoder(decoder:IDecoder):Boolean
		{
			var type:String=decoder.getType();
			
			if(decoders[type]==null){
				return false;
			}
			
			decoder.manager=null;
			
			decoders[type]=null;
			delete decoders[type];
			
			return true;
		}
		
		public function getDecoder(type:String):IDecoder
		{
			return this.decoders[type];
		}
		
		public function onData(server:INetSender, commandInfo:CommandInfo):void
		{
			var type:String=commandInfo.command;
			var decoder:IDecoder=this.getDecoder(type);
			if(decoder)return;
			decoder.onData(commandInfo,server);
			decoder.decode();
		}
	}
}